package main

import (
	"fmt"
	"net/http"
)

func setCookie(w http.ResponseWriter, r *http.Request) {

	cookie := http.Cookie{
		Name:     "user",
		Value:    "admin",
		HttpOnly: true,
		MaxAge:   60,
	}
	cookie2 := http.Cookie{
		Name:     "user2",
		Value:    "admin2",
		HttpOnly: true,
	}
	//方式1:
	//w.Header().Set("Set-Cookie", cookie.String())
	//// cookie可以设置多个
	//w.Header().Add("Set-Cookie", cookie2.String())

	// 方式2:
	http.SetCookie(w, &cookie)
	http.SetCookie(w, &cookie2)
}
func getCookie(w http.ResponseWriter, r *http.Request) {
	// 方式1:
	//cookie := r.Header["Cookie"]

	// 方式2: 只能通过名字得到一个了
	cookie, _ := r.Cookie("user")
	fmt.Println("得到的cookie有:", cookie)
}

func main() {
	http.HandleFunc("/setCookie", setCookie)
	http.HandleFunc("/getCookie", getCookie)

	http.ListenAndServe(":8080", nil)
}
